<?php
/**
 * Cellphonezone
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Cellphonezone
 * @package     Cellphonezone_Magemenu
 * @copyright   Copyright (c) 2011 Cellphonezone
 * @Author $Id: Navigation.php 505 2011-09-14 06:08:04Z lon $
*/
class Cellphonezone_Magemenu_Block_Navigation extends Mage_Core_Block_Template
{
    protected $_categoryInstance = null;
	protected $my = 0;
    /**
     * Array of level position counters
     *
     * @var array
     */
    protected $_itemLevelPositions = array();

    protected function _construct()
    {
        $this->addData(array(
            'cache_lifetime'    => false,
            'cache_tags'        => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG),
        ));
    }

    /**
     * Get Key pieces for caching block content
     *
     * @return array
     */
    public function getCacheKeyInfo()
    {
        return array(
            'CATALOG_NAVIGATION',
            Mage::app()->getStore()->getId(),
            Mage::getDesign()->getPackageName(),
            Mage::getDesign()->getTheme('template'),
            Mage::getSingleton('customer/session')->getCustomerGroupId(),
            'template' => $this->getTemplate(),
            $this->getCurrenCategoryKey()
        );
    }

    public function getCurrenCategoryKey()
    {
        if ($category = Mage::registry('current_category')) {
            return $category->getPath();
        } else {
            return Mage::app()->getStore()->getRootCategoryId();
        }
    }

    /**
     * Get catagories of current store
     *
     * @return Varien_Data_Tree_Node_Collection
     */
    public function getStoreCategories()
    {
        $helper = Mage::helper('catalog/category');
        return $helper->getStoreCategories();
    }

    /**
     * Retrieve child categories of current category
     *
     * @return Varien_Data_Tree_Node_Collection
     */
    public function getCurrentChildCategories()
    {
        $layer = Mage::getSingleton('catalog/layer');
        $category   = $layer->getCurrentCategory();
        /* @var $category Mage_Catalog_Model_Category */
        $categories = $category->getChildrenCategories();
        $productCollection = Mage::getResourceModel('catalog/product_collection');
        $layer->prepareProductCollection($productCollection);
        $productCollection->addCountToCategories($categories);
        return $categories;
    }

    /**
     * Checkin activity of category
     *
     * @param   Varien_Object $category
     * @return  bool
     */
    public function isCategoryActive($category)
    {
        if ($this->getCurrentCategory()) {
            return in_array($category->getId(), $this->getCurrentCategory()->getPathIds());
        }
        return false;
    }

    protected function _getCategoryInstance()
    {
        if (is_null($this->_categoryInstance)) {
            $this->_categoryInstance = Mage::getModel('catalog/category');
        }
        return $this->_categoryInstance;
    }

    /**
     * Get url for category data
     *
     * @param Mage_Catalog_Model_Category $category
     * @return string
     */
    public function getCategoryUrl($category)
    {
        if ($category instanceof Mage_Catalog_Model_Category) {
            $url = $category->getUrl();
        } else {
            $url = $this->_getCategoryInstance()
                ->setData($category->getData())
                ->getUrl();
        }

        return $url;
    }

    /**
     * Return item position representation in menu tree
     *
     * @param int $level
     * @return string
     */
    protected function _getItemPosition($level)
    {
        if ($level == 0) {
            $zeroLevelPosition = isset($this->_itemLevelPositions[$level]) ? $this->_itemLevelPositions[$level] + 1 : 1;
            $this->_itemLevelPositions = array();
            $this->_itemLevelPositions[$level] = $zeroLevelPosition;
        } elseif (isset($this->_itemLevelPositions[$level])) {
            $this->_itemLevelPositions[$level]++;
        } else {
            $this->_itemLevelPositions[$level] = 1;
        }

        $position = array();
        for($i = 0; $i <= $level; $i++) {
            if (isset($this->_itemLevelPositions[$i])) {
                $position[] = $this->_itemLevelPositions[$i];
            }
        }
        return implode('-', $position);
    }

    /**
     * Render category to html
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int Nesting level number
     * @param boolean Whether ot not this item is last, affects list item class
     * @param boolean Whether ot not this item is first, affects list item class
     * @param boolean Whether ot not this item is outermost, affects list item class
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @param boolean Whether ot not to add on* attributes to list item
     * @return string
     */
    protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false, $isOutermost = false, 
    $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false, $activeChildren = array())
    {
        if (!$category->getIsActive()) {
            return '';
        }
        $html = array();
		//get active children
        if (empty($activeChildren) && $level==0)
        {
        	$activeChildren = $this->getActiveChildren($category);
        }
        
        $activeChildrenCount = count($activeChildren);
        $hasActiveChildren = ($activeChildrenCount > 0);

        // prepare list item html classes
        if (!$hasActiveChildren && $level!=0)
        {
        	
        	$noSubHtml = $isFirst ? '<li style="border:0px;">' : '<li>';
        	$noSubHtml .= '<a href="'.$this->getCategoryUrl($category).'">';
        	$noSubHtml .= '<span>' . $this->escapeHtml($category->getName()) . '</span>';
        	$noSubHtml .= '</a></li>';
        	return $noSubHtml;
        }
        else 
        {
        	$classes = array();
	        $classes[] = 'level' . $level;
	        $classes[] = 'nav-' . $this->_getItemPosition($level);
	        $linkClass = '';
	        if ($isOutermost && $outermostItemClass) {
	            $classes[] = $outermostItemClass;
	            $linkClass = ' class="'.$outermostItemClass.'"';
	        }
	        if ($this->isCategoryActive($category)) {
	            $classes[] = 'active';
	        }
	        if ($isFirst) {
	            $classes[] = 'first';
	        }
	        if ($isLast) {
	            $classes[] = 'last';
	        }
	        if ($hasActiveChildren) {
	            $classes[] = 'parent';
	        }
	
	        // prepare list item attributes
	        $attributes = array();
	        if (count($classes) > 0) {
	            $attributes['class'] = implode(' ', $classes);
	        }
	        if ($hasActiveChildren && !$noEventAttributes) {
	             $attributes['onmouseover'] = 'toggleMenu(this,1)';
	             $attributes['onmouseout'] = 'toggleMenu(this,0)';
	        }
	
	        // assemble list item with attributes
	        $htmlLi = '<li';
	        foreach ($attributes as $attrName => $attrValue) {
	            $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
	        }
	        $htmlLi .= '>';
			$html[] = $htmlLi;
	
	        $html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
	        $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
	        $html[] = '</a>';
        }        
		// render children
        $htmlChildren = '';
        $htmlNoChildren = '';
        $j = 0;
        $k = 0 ;
        foreach ($activeChildren as $child) {
        	$subActiveChildren = $this->getActiveChildren($child);
        	$hasSubActiveChildren = count($subActiveChildren) > 0;        
        	if (!$hasSubActiveChildren && $level == 0)
        	{
        		$activeChildrenCount--;
        		$htmlNoChildren .= $this->_renderCategoryMenuItemHtml($child,($level + 1),false, ($k==0));
        		$k++;
        	}
        	else 
        	{        		
        		$htmlChildren .= $this->_renderCategoryMenuItemHtml(
                $child,
                ($level + 1),
                ($j == $activeChildrenCount - 1),
                ($j == 0),
                false,
                $outermostItemClass,
                $childrenWrapClass,
                $noEventAttributes,
                $subActiveChildren
	            );
	            if ($level > 0 && $j >6)
	            {
	            	if ($j < $activeChildrenCount - 1)
	            	{
	            		$htmlChildren .= '<li><a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'><b class="gray">View All</b></a></li>';
	            	}
	            	break;
	            }
	            $j++;
        	}
            
        }
        if (!empty($htmlChildren) || !empty($htmlNoChildren)) {
            if ($level==0) {
            	$maxCount = 5;
            	$setWidth = $activeChildrenCount > $maxCount ?  $maxCount* 189+20 : (!empty($htmlNoChildren) ? ($activeChildrenCount+1)*189+20 : $activeChildrenCount*189+20);
				$html[]= '<div class="menu children'.$activeChildrenCount.'" style="width:'.$setWidth.'px;">';
				$html[]='<div class="shadow-round-left">';
				$html[]='<div class="shadow-round-right">';
				$html[]='<div class="red-border">';
				$html[]='<div class="shadow-bottom">';
				$html[]='<div class="nav-inner clearfix">';
			}
			if (!empty($htmlChildren))
			{				
				if ($childrenWrapClass) {
	                $html[] = '<div class="' . $childrenWrapClass . '">';
	            }
	            $setLevel0Width = isset($setWidth) ? (!empty($htmlNoChildren) ? 'style="width:'.($setWidth-189-20).'px;"' : 'style="width:'.($setWidth-20).'px;"') : '';
	            $html[] = '<ul class="level' . $level . '" '. $setLevel0Width .'>';
	            $html[] = $htmlChildren;
	            $html[] = '</ul>';
	            if ($childrenWrapClass) {
	                $html[] = '</div>';
	            }
			}
		     if ($level==0) {
		     	if (!empty($htmlNoChildren))
		     	{
		     		$floatStyle = !empty($htmlChildren) ? 'style="float:right;"' : 'style="float:left;"';
		     		$html[] = '<div class="nav_no_sub" '.$floatStyle.'><ul>'.$htmlNoChildren.'</ul></div>';
		     	}
				$html[]= $this->getLayout()->createBlock('cms/block')->setBlockId('banner_'.$this->_itemLevelPositions[0])->toHtml();
				$html[]= '</div></div></div></div></div></div>';	
			}
        }

        $html[] = '</li>';
		
        $html = implode("\n", $html);
        return $html;
    }

    
    protected function getActiveChildren($category)
    {
      // get all children
      if (Mage::helper('catalog/category_flat')->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
            $childrenCount = count($children);
        } else {
            $children = $category->getChildren();
            $childrenCount = $children->count();
        }
        $hasChildren = ($children && $childrenCount);

        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
            }
        }
        return $activeChildren;
    }
    /**
     * Render category to html
     *
     * @deprecated deprecated after 1.4
     * @param Mage_Catalog_Model_Category $category
     * @param int Nesting level number
     * @param boolean Whether ot not this item is last, affects list item class
     * @return string
     */
    public function drawItem($category, $level = 0, $last = false)
    {
        return $this->_renderCategoryMenuItemHtml($category, $level, $last);
    }

    /**
     * Enter description here...
     *
     * @return Mage_Catalog_Model_Category
     */
    public function getCurrentCategory()
    {
        if (Mage::getSingleton('catalog/layer')) {
            return Mage::getSingleton('catalog/layer')->getCurrentCategory();
        }
        return false;
    }

    /**
     * Enter description here...
     *
     * @return string
     */
    public function getCurrentCategoryPath()
    {
        if ($this->getCurrentCategory()) {
            return explode(',', $this->getCurrentCategory()->getPathInStore());
        }
        return array();
    }

    /**
     * Enter description here...
     *
     * @param Mage_Catalog_Model_Category $category
     * @return string
     */
    public function drawOpenCategoryItem($category) {
        $html = '';
        if (!$category->getIsActive()) {
            return $html;
        }
		 $html.= '<li';

        if ($this->isCategoryActive($category)) {
            $html.= ' class="active"';
        }

        $html.= '>'."\n";
        $html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";

        if (in_array($category->getId(), $this->getCurrentCategoryPath())){
            $children = $category->getChildren();
            $hasChildren = $children && $children->count();

            if ($hasChildren) {
                $htmlChildren = '';
                foreach ($children as $child) {
                    $htmlChildren.= $this->drawOpenCategoryItem($child);
                }

                if (!empty($htmlChildren)) {
                    $html.= '<ul>'."\n"
                            .$htmlChildren
                            .'</ul>';
                }
            }
        }
        $html.= '</li>'."\n";
        return $html;
    }

    /**
     * Render categories menu in HTML
     *
     * @param int Level number for list item class to start from
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @return string
     */
    public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
    {
        $activeCategories = array();
        foreach ($this->getStoreCategories() as $child) {
            if ($child->getIsActive()) {
                $activeCategories[] = $child;
            }
        }
        $activeCategoriesCount = count($activeCategories);
        $hasActiveCategoriesCount = ($activeCategoriesCount > 0);

        if (!$hasActiveCategoriesCount) {
            return '';
        }

        $html = '';
        $j = 0;
        foreach ($activeCategories as $category) {
            $html .= $this->_renderCategoryMenuItemHtml(
                $category,
                $level,
                ($j == $activeCategoriesCount - 1),
                ($j == 0),
                true,
                $outermostItemClass,
                $childrenWrapClass,
                true
            );
            $j++;
        }

        return $html;
    }

}
